package com.example.smartcity_40.service.apter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.smartcity_40.Outpatient.OutpatientActivity;
import com.example.smartcity_40.R;
import com.example.smartcity_40.house.HouseActivity;
import com.example.smartcity_40.lawyer.LawyerActivity;
import com.example.smartcity_40.service.bean.ServiceIMGBean;
import com.example.smartcity_40.service.takeout.TakeOutActivity;
import com.example.smartcity_40.tool.Tool;

import java.util.List;

public class ServiceIMGApter  extends  RecyclerView.Adapter<ServiceIMGApter.Myhot> {

    private Context context;
    private List<ServiceIMGBean.RowsDTO> list;

    public ServiceIMGApter(Context context) {
        this.context = context;
    }

    public void setDate(List<ServiceIMGBean.RowsDTO> list){
        this.list = list;
    }
    @NonNull
    @Override
    public Myhot onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.l_item_service, null);
        return new ServiceIMGApter.Myhot(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull Myhot holder, int position) {
        try {
            String imgUrl = list.get(position).getImgUrl();
            if(!TextUtils.isEmpty(imgUrl)){
                Glide.with(context).load(Tool.url+imgUrl).into(holder.imgurl);
            }

            String serviceName = list.get(position).getServiceName();
            if(!TextUtils.isEmpty(serviceName)){
                holder.serviceName.setText(serviceName);
            }
            int id = list.get(position).getId();
            holder.relayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (id){
                        case 5  :
                            //门诊预约
                            context.startActivity(new Intent(context, OutpatientActivity.class));
                            break;
                        case 20 :
                            //找房子
                            context.startActivity( new Intent(context, HouseActivity.class));
                            break;
                        case 23 :
                            //宠物医院
                            break;
                        case 19 :
                            //外卖
                            Intent intent = new Intent(context, TakeOutActivity.class);
                            context.startActivity(intent);
                            break;
                        case 99 :
                            //律师
                            context.startActivity( new Intent(context, LawyerActivity.class));
                            break;
                        default:
                            Toast.makeText(context,"待开发",Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    class Myhot extends RecyclerView.ViewHolder{

        private final TextView serviceName;
        private final ImageView imgurl;
        private final RelativeLayout relayout;

        public Myhot(@NonNull View itemView) {
            super(itemView);
            imgurl = itemView.findViewById(R.id.imgurl);
            serviceName = itemView.findViewById(R.id.serviceName);
            relayout = itemView.findViewById(R.id.relayout);
        }
    }
}
